import utils from '@/mixins/utils.js';
import { resolve } from 'url';
export default {
  name: 'card_device',
  mixins: [utils],
  data() {
    return {
      deviceConfigList: [],
      currentEditItem: null,
      parkConfigInfo: null
    };
  },
  created() {
    this.getParkConfigInfoByUUID();
    this.getDeviceConfigList();
  },
  methods: {
    getParkConfigInfoByUUID() {
      var self = this;
      API.fetchGet('/operatorPark/getOperatorParkConfigInfo', {
        parkID: this.$route.query.parkId
      }).then(
        res => {
          this.parkConfigInfo = res.data;
        },
        err => {}
      );
    },
    getDeviceConfigList() {
      const self = this;
      API.fetchGet('/operatorPark/CardMachine/getDeviceConfigList', {
        parkID: this.$route.query.parkId
      }).then(
        res => {
          if (!this.deviceConfigList.length && res.data.length) {
            this.deviceConfigList = res.data.map(item => {
              item.uiOpt = {
                isEdit: false,
                isShowDetail: false
              };
              return item;
            });
          }

          if (this.deviceConfigList.length && res.data.length) {
            res.data.forEach(item => {
              const same_item = this.deviceConfigList.filter(sitem => {
                return sitem.channelConfigVo.channelID === item.channelConfigVo.channelID;
              });
              item.uiOpt = same_item[0].uiOpt;
              item = Object.assign(same_item[0], item);
            });
            this.deviceConfigList = res.data;
          }
        },
        err => {}
      );
    },
    editDeviceItem(item) {
      this.currentEditItem = JSON.parse(JSON.stringify(item));
      this.deviceConfigList.forEach(item => {
        item.uiOpt.isEdit = false;
      });
      item.uiOpt.isEdit = true;
    },
    cancelEdit(list, index) {
      list.splice(index, 1, this.currentEditItem);
    },
    changeDeviceItem(item) {
      API.fetchPost('/operatorPark/CardMachine/processDevice/1', item).then(
        res => {
          if (res.status === 1) {
            this.showToast('设备修改成功', 1, 1500);
          } else {
            this.showToast(res.message, 1, 2000);
          }
          item.uiOpt.isEdit = false;
          this.getDeviceConfigList();
        },
        err => {
          console.log(err);
        }
      );
    },
    getTideChannelStatus(curItem) {
      let params = {
        parkID: curItem.channelConfigVo.parkGlobalID,
        channelID: curItem.channelConfigVo.channelID
      };
      return API.fetchGet('/operatorPark/checkChannel', params);
    },
    delDevice(item, index) {
      this.getTideChannelStatus(item).then(res => {
        if (res.status === 0 && res.resultCode === 0) return;

        if (res.status === 1 && (res.resultCode === -1 || res.resultCode === -3)) {
          this.showToast('该通道存在用户上班中，请在用户下班后删除', 1, 2000);
          return;
        }

        this.$confirm('您确定要删除本设备？', '删除设备', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          customClass: 'mgr-msg-box',
          confirmButtonClass: 'el-button el-button--danger'
        }).then(() => {
          API.fetchPost('/operatorPark/CardMachine/processDevice/2', item).then(
            res => {
              if (res.status === 1) {
                this.showToast('删除设备成功', 1, 1500);
                this.getDeviceConfigList();
              } else {
                this.showToast('删除设备失败', 2, 1500);
              }
              item.uiOpt.isEdit = false;
            },
            err => {
              console.log(err);
            }
          );
        });
      });
    },
    //进入设备后台
    toDeviceBackend(parkID, ytjId) {
      let _this = this;
      let params = {
        parkID: parkID,
        ytjId: ytjId
      };
      API.fetchGet('operatorPark/get666BackendUrl', params).then(
        res => {
          let data = res.data;
          if (data && data.url) {
            window.open(data.url, '_blank');
          } else {
            _this.showToast('设备暂不支持该功能', 0);
          }
        },
        err => {}
      );
    }
  }
};
